﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;

namespace YJKStairs.Form
{
	public partial class StairsClearHeightDimensionForm : System.Windows.Forms.Form
	{
		public StairsClearHeightDimensionForm(ExternalCommandData cmdData)
		{
			this.m_Revit = cmdData;
			this.m_rvtDoc = this.m_Revit.Application.ActiveUIDocument.Document;
			this.InitializeComponent();
		}

		private void StairsClearHeightDimension_Load(object sender, EventArgs e)
		{
			base.Icon = Resource.YArchitechIcon;
			this.InitDimTypeData();
			this.InitComboDimStyle(this.comboDimStyle);
			this.ReadData();
		}

		private void InitDimTypeData()
		{
			foreach (DimensionType dimensionType in StairsDimensionStyleMgr.GetDimensionTypeByStyleType(0, this.m_rvtDoc))
			{
				string name = dimensionType.Name;
				if (!this.m_arDimType.ContainsKey(name))
				{
					this.m_arDimType.Add(name, dimensionType);
				}
				else
				{
					this.m_arDimType[name] = dimensionType;
				}
			}
			this.m_strDimType.AddRange(this.m_arDimType.Keys.ToList<string>());
			this.m_strDimType.Sort();
		}

		private void InitComboDimStyle(System.Windows.Forms.ComboBox comboDimStyle)
		{
			comboDimStyle.DataSource = this.m_strDimType;
		}

		private void SaveData()
		{
			FileStream fileStream = new FileStream(Path.GetTempPath() + "StairsClearHeightDimensionForm.dat", FileMode.Create);
			StreamWriter streamWriter = new StreamWriter(fileStream);
			streamWriter.WriteLine(this.comboDimStyle.Text);
			streamWriter.Flush();
			streamWriter.Close();
			fileStream.Close();
		}

		private void ReadData()
		{
			int selectedIndex = 0;
			string path = Path.GetTempPath() + "StairsClearHeightDimensionForm.dat";
			if (File.Exists(path))
			{
				FileStream fileStream = new FileStream(path, FileMode.Open);
				StreamReader streamReader = new StreamReader(fileStream);
				streamReader.BaseStream.Seek(0L, SeekOrigin.Begin);
				string value = streamReader.ReadLine();
				int num = this.comboDimStyle.Items.IndexOf(value);
				if (-1 != num)
				{
					selectedIndex = num;
				}
				streamReader.Close();
				fileStream.Close();
			}
			this.comboDimStyle.SelectedIndex = selectedIndex;
		}

		private ElementId GetDimTypeByName(string sTypeName)
		{
			return this.m_arDimType[sTypeName].Id;
		}

		public ElementId GetCurSelectDimTypeId()
		{
			return this.m_curDimTypeId;
		}

		private void btnOK_Click(object sender, EventArgs e)
		{
			string sTypeName = this.comboDimStyle.SelectedItem as string;
			this.m_curDimTypeId = this.GetDimTypeByName(sTypeName);
		}

		private int GetMaxDimNamePixelLength()
		{
			int num = 0;
			foreach (string text in this.m_strDimType)
			{
				int num2 = (int)base.CreateGraphics().MeasureString(text, this.Font).Width;
				if (num2 > num)
				{
					num = num2;
				}
			}
			return num;
		}

		private void comboDimStyle_DropDown(object sender, EventArgs e)
		{
			if (this.m_strDimType.Count > 0)
			{
				this.comboDimStyle.DropDownWidth = this.GetMaxDimNamePixelLength();
			}
		}

		private void StairsClearHeightDimensionForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			this.SaveData();
		}

		private ExternalCommandData m_Revit;

		private Dictionary<string, DimensionType> m_arDimType = new Dictionary<string, DimensionType>();

		private List<string> m_strDimType = new List<string>();

		private Document m_rvtDoc;

		private ElementId m_curDimTypeId;
	}
}
